package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import connection.DbConnection;

import bean.Answer;
import bean.Question;

import model.CreateSAnswerImpl;
import model.CreateSQuestionImpl;
public class SubmitSAServlet extends HttpServlet {


	

	/**
	 * 
	 */
	private static final long serialVersionUID = -1731304887541490383L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost (request, response);
	}
	
	public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		System.out.println("SubmitSAServlet run");
		
		response.setContentType("text/html");
		response.setCharacterEncoding("GB18030");
		String answer = request.getParameter("answer");
		String quesid = request.getParameter("qid");
		String pollid = request.getParameter("pid");
		String pollver = request.getParameter("pver");
		String username = request.getParameter("username");
		
		
		Answer answers = new Answer();
		answers.setText(answer);
		answers.setQuestionID(Integer.parseInt(quesid));
		answers.setPollid(Integer.parseInt(pollid));
		answers.setVersion(Integer.parseInt(pollver));
		answers.setUsernameorDeviceID(username);
		
		
		DbConnection db = new DbConnection();
		String sql;
		boolean submitAnswerPreviously = false;
		
		// Check if already submit answer
		try {
		sql = "select answerid from answer where pollid = "+pollid+" and questionid = "+quesid+" and usernameordeviceid = '"+username+"'";
		System.out.println("I AM HERE IN SubmitMC SQL: "+sql);
		Connection conn = db.getConn();
		ResultSet resSet;
		
			resSet = conn.prepareStatement(sql).executeQuery();
		
		//ResultSet resSet = db.doSelect(sql);
		
		System.out.println("Apple");
		
			while (resSet.next()) {
				System.out.println("insdie why?"+resSet.getString("answerid"));
				submitAnswerPreviously = true;
			}
		} catch (Exception e) {
			System.out.println("SQL ERROR IN SubmitMCServlet");
		}
		System.out.println("banana submitAnswerPreviously: "+submitAnswerPreviously);
		
		
		
		// Split into update/insert
		if (submitAnswerPreviously){
			//CreateSAnswerImpl createanswer = new CreateSAnswerImpl();
			//createanswer.updatequestion(answers);
			if (request.getSession().getAttribute("lastAnswerId") != null) {
				sql = "update answer set text='"+answers.getText()+"' where questionid = "+answers.getQuestionID()+" and usernameordeviceid = '"+answers.getUsernameorDeviceID()+"' and answerid="+request.getSession().getAttribute("lastAnswerId");
			} else {
				sql = "update answer set text='"+answers.getText()+"' where questionid = "+answers.getQuestionID()+" and usernameordeviceid = '"+answers.getUsernameorDeviceID()+"' and answerid="+334;
			}
			System.out.println(sql);
			db.doUpdate(sql);
		}
		else{
			CreateSAnswerImpl createanswer = new CreateSAnswerImpl();
			createanswer.createanswer(answers);
			
			sql = "select max(answerid) from answer";
			System.out.println(sql);
			ResultSet rs = db.doSelect(sql);
			try {
				while (rs.next()) {
					request.getSession().setAttribute("lastAnswerId",rs.getString(1));
				}
			} catch (Exception e) {
				System.out.println("SQL error in submitsaservlet");
			}
			
		}
		String forward= "Answerform.jsp";
		
		response.sendRedirect(forward);
	}
}
